import os
import time

import requests
from selenium import webdriver


drive = webdriver.Chrome()

url = "https://image.baidu.com/"
drive.get(url=url)
time.sleep(2)

search = drive.find_element_by_xpath('//*[@id="kw"]')
search.send_keys('小方')
time.sleep(2)

conmmit = drive.find_element_by_xpath('//*[@id="homeSearchForm"]/span[2]/input')
conmmit.click()
time.sleep(2)
# 创建一个文件夹
if not os.path.exists('./小方'):
    os.mkdir('./小方')

# img = drive.find_elements_by_tag_name('img')

'''
//*[@id="imgid"]/div/ul/li[4]/div[1]/div/a/img
'''
print('开始下载>>>>>>>>')
img = drive.find_elements_by_xpath('//*[@id="imgid"]//img')
name = 1
for i in img:
    src = i.get_attribute('src')
    # name = str(time.time())
    img_name = str(name)+'.jpg'
    img_data = requests.get(url=src).content
    img_path = '小方/' + img_name
    with open(img_path, 'wb') as fp:
        fp.write(img_data)
        print(img_name, '下载成功！！！')
    name += 1




drive.quit()
